<?php
/**
 * Dao of Comment
 *
 * @author fsqijy
 */


require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';

class CommentDao extends BaseDao{

    protected $_name;

    public function  _setup() {
        $this->_name = $this->prefix() . "comments";
        parent::_setup();
    }
    
    public function getCommentById($id){
    	$select = $this->db_r->select();
    	$select->from('comments as c');
    	$select->joinLeft('comment_replies as r', 'c.id=r.comment_id', array('r.content as rcontent', 'r.id as reply_id','r.created as rcreated'));
    	$select->joinLeft('users as u', 'c.user_id=u.id', array('u.name'));
    	$select->joinLeft('products as p', 'c.product_id=p.id', array('p.title'));
    	$select->joinLeft('admins as a', 'a.id=r.reply_id', array('a.username as replier'));
    	$select->where('c.is_delete=0 and c.id=?',$id);
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }
    
    public function saveCommentsReply($param){
    	$select = $this->db_r->select();
    	$select->from('comment_replies','id');
    	$select->where('comment_id=?',$param['comment_id']);
    	$commentReply = $this->db_r->fetchRow($select->__toString());
    	if( !empty($commentReply) ){
	        $where = $this->db_w->quoteInto('comment_id = ?', $param['comment_id']);
	        return $this->updateData($set,$where);
    	}else{
    		return $this->insert($param);
    	}
        return false;
    }
    
    public function deleteCommentById($id){
    	return $this->db_w->update($this->_name,array('is_delete'=>1),"id={$id}");
    }
    
}
